// 路由
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import home from '@/component/home.vue'
import login from '@/component/login.vue'

// home的子组件
import categories from '@/views/goods/categories.vue'
import goods from '@/views/goods/goods.vue'
import params from '@/views/goods/params.vue'
import orders from '@/views/orders/orders.vue'
import reports from '@/views/reports/reports.vue'
import rights from '@/views/roles/rights.vue'
import roles from '@/views/roles/roles.vue'
import users from '@/views/user/users.vue'

// 路由规则
const routes = [{
  path: '/',
  redirect: '/login',
}, {
  path: '/home',
  component: home,
  children: [{
    path: '/categories',
    component: categories,
  }, {
    path: '/goods',
    component: goods,
  }, {
    path: '/params',
    component: params,
  }, {
    path: '/orders',
    component: orders,
  }, {
    path: '/reports',
    component: reports,
  }, {
    path: '/rights',
    component: rights,
  }, {
    path: '/roles',
    component: roles,
  }, {
    path: '/users',
    component: users,
  }]
}, {
  path: '/login',
  component: login
}]

const router = new VueRouter({
  // 默认
  mode: 'hash',
  routes
})

// 路由拦截(前置守卫)
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  
  // 是login路由值
  if (to.path=='/login') {
   return next()
  }
  // 是不是有token  有token就放行 没有就去login
  const token = window.sessionStorage.getItem('token')
if(!token){
return next('/login')
}

next()
})

// 解决掉了问题会出现兼容性
    const originalPush = VueRouter.prototype.push;
    VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
    };

export default router
